<template>
	<view class="main">
		<v-tabs :scroll="false" activeColor="#FF6543" lineColor="#FF6543" v-model="current" :tabs="tabs"
			@change="changeTab"></v-tabs>
		<view v-if="current==0" class="flex align warp" style="flex-wrap: wrap;">
			<view class="img-div" v-for="(item,index) in list" :key="index">
				<image :src="item.url" @click="toPre(item.url)"></image>
				<view class="btn" @click="todel(item.id)">
					<uni-icons type="clear" size="30" color="red"></uni-icons>
				</view>
			</view>
		</view>
		<view v-if="current==1" class="flex align warp" style="flex-wrap: wrap;">
			<view v-for="(item,index) in list" :key="index" class="img-div">
				<video :src="item.url" controls></video>
				<view class="btn" @click="todel(item.id)">
					<uni-icons type="clear" size="30" color="red"></uni-icons>
				</view>
			</view>
		</view>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		<view class="main-btn" @click="toUrl()">上传{{current==0?'图片':'视频'}}</view>
	</view>
</template>

<script>
	import {
		companyMaterialLists,
		materialAdd,
		companyMateriaDelete
	} from '@/api/apiData.js';
	import {
		uploadOss
	} from '@/utils/oss/oss.js'
	export default {
		data() {
			return {
				current: 0,
				tabs: ['图片素材', '视频素材'],
				type: 1,
				group_id: '',
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				imgList: [],
				urlsArr: [],
				urlsArrN: []
			};
		},
		onLoad(option) {
			this.group_id = option.id
			// this.getlist()
		},
		onShow() {
			this.page = 0
			this.list = []
			this.getlist()
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			todel(id) {
				let that = this
				uni.showModal({
					title: "提示",
					content: "确定要删除吗？",
					showCancel: true,
					success: res => {
						if (res.confirm) {
							that.delCom(id)
						} else {

						}
					}

				})
			},
			async delCom(id) {
				const result = await companyMateriaDelete({
					ids: id,
				})
				if (result.code == 1) {
					uni.showToast({
						title: result.msg
					})
					setTimeout(() => {
						this.page = 0
						this.list = []
						this.getlist()
					}, 500);
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'error'
					})
				}
			},
			toUrl() {
				if (this.current == 0) { //上传图片
					this.toUploadImg()
				} else { //上传视频
					this.toUploadVideo()
					// uni.navigateTo({
					// 	url: '/pages/manage/upload?type=' + this.current + '&id=' + this.group_id
					// })
				}

			},
			toUploadImg() {
				let that = this
				uni.showLoading({
					title: "loading",
					mask: true
				})
				uni.chooseMedia({
					count: 9,
					mediaType: ['image'],
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera', 'album'], //从相册选择
					success: (res) => {
						console.log(res)
						// OSS上传(得到在oss文件夹下照片路径)
						let fileArr = res.tempFiles
						let length = fileArr.length

						fileArr.forEach((item, index) => {
							console.log(item.tempFilePath)
							that.toOss(item.tempFilePath, index, length)
						})
					},
					fail() {
						uni.hideLoading()
					}
				})
			},
			toUploadVideo() {
				let that = this
				uni.showLoading({
					title: "loading",
					mask: true
				})
				uni.chooseMedia({
					count: 9,
					mediaType: ['video'],
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera', 'album'], //从相册选择
					success: (res) => {
						let fileArr = res.tempFiles
						let length = fileArr.length
						fileArr.forEach((item, index) => {
							console.log(item.tempFilePath)
							that.toOss(item.tempFilePath, index, length)
						})
					},
					fail() {
						uni.hideLoading()
					}
				})
			},
			async toOss(item, index, length) {
				let ossPath = await uploadOss({
					filePathFore: 'img/' + index + '/',
					filePath: item,
				})
				this.urlsArr.push('https://jgdrxt.oss-cn-shanghai.aliyuncs.com/' + ossPath)
				console.log(this.urlsArr.length)
				console.log(length)
				if (this.urlsArr.length == length) {
					console.log(this.urlsArr)
					this.urlsArr.forEach(im => {
						this.urlsArrN.push({
							name: '',
							url: im
						})
					})
					this.toComf()
				}
			},
			async toComf() {
				uni.showLoading({
					title: "loading",
					mask: true
				})
				const result = await materialAdd({
					group_id: this.group_id,
					type: this.type,
					url: this.urlsArrN
				})
				if (result.code == 1) {
					uni.hideLoading()
					uni.showToast({
						title: result.msg
					})
					setTimeout(() => {
						this.urlsArr = []
						this.urlsArrN = []
						this.page = 0
						this.list = []
						this.getlist()
					}, 500);
				} else {
					uni.hideLoading()
					uni.showToast({
						title: result.msg,
						icon: 'error'
					})
				}
			},
			toDownImg(url) {
				uni.showLoading({
					title: '下载中...',
				})
				uni.downloadFile({
					//视频信息的Url
					url: url,
					success: function(res) {
						// 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
						if (res.statusCode === 200) {
							console.log(res.tempFilePath);
							uni.saveVideoToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function(res) {
									console.log(res.errMsg)
									uni.hideLoading();
									uni.showToast({
										title: '下载成功'
									})
								}
							})
						}
					},
					fail() {
						uni.showToast({
							title: '下载失败',
							icon: 'error'
						})
						uni.hideLoading();
					}
				})

			},
			toDown(url) {
				uni.showLoading({
					title: '下载中...',
				})
				uni.downloadFile({
					//视频信息的Url
					url: url,
					success: function(res) {
						// 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
						if (res.statusCode === 200) {
							console.log(res.tempFilePath);
							uni.saveVideoToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function(res) {
									console.log(res.errMsg)
									uni.hideLoading();
									uni.showToast({
										title: '下载成功'
									})
								}
							})
						}
					},
					fail() {
						uni.showToast({
							title: '下载失败',
							icon: 'error'
						})
						uni.hideLoading();
					}
				})

			},
			toPre(i) {
				uni.previewImage({
					urls: [i],
					current: 0
				})
			},
			changeTab(e) {
				console.log(e)
				this.type = e + 1
				this.page = 0
				this.list = []
				this.getlist()
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					group_id,
					type
				} = that;
				let obj = {
					size: offset,
					page: page + 1,
					group_id,
					type
				};
				that.status = 'loading';
				if (totalPage > page) {
					let result = await companyMaterialLists(obj);
					if (result.code == 1) {
						that.page = page + 1;
						if (result.data.total == 0) {
							console.log((888))
							that.empty = true;
							that.status = 'nomore';
							return;
						}
						that.empty = false;
						setTimeout(() => {
							that.status = 'loadmore';
						}, 500);
						that.list = list.concat(result.data.list);
						that.totalPage = Math.ceil(result.data.total / offset);
					}
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		padding-bottom: 80rpx;
	}

	.btns {
		background: #FF6543;
		width: 120rpx;
		text-align: center;
		line-height: 48rpx;
		border-radius: 12rpx;
		color: white;
		margin: 0 auto;
		font-size: 26rpx;
	}

	.img-div {
		width: 33.33%;
		text-align: center;
		margin-top: 20rpx;
		position: relative;

		image,video {
			width: 92%;
			height: 200rpx;
			object-fit: cover;
			background: white;
		}
	}

	.btn {
		position: absolute;
		right: 0;
		top: -20rpx;
	}

	.list {
		margin: 24rpx;
		border-radius: 12rpx;

		image {
			width: 150rpx;
			height: 150rpx;
		}





		.list-info {
			margin-left: 20rpx;
			width: 73%;

			.name {
				font-size: 32rpx;
				font-weight: bold;
			}

			.time {
				color: gray;
				font-size: 28rpx;
				margin-top: 20rpx;
			}
		}




		.video-div {
			margin-bottom: 20rpx;
			background: white;
			padding: 16rpx 24rpx;

			video {
				width: 100%;
				height: 250rpx;
				object-fit: fill;
			}

			.name {
				text-align: center;
				margin-bottom: 20rpx;
				font-weight: bold;
			}

			.btn {
				margin: 0 auto;
			}
		}
	}

	.main-btn {
		font-size: 34rpx;
		font-weight: bold;
		color: white;
		text-align: center;
		background: #FF6543;
		line-height: 108rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
</style>